package com.devil.traffic.task;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import com.devil.traffic.tools.StreamTool;


public class DoTaskMethod {

	public String getSimpleTrafficRecord(String strSerailNum) throws IOException, Exception {
		//实例化HttpClient
		HttpClient client = new HttpClient();
		//Stream页面里面有Host地址 端口是80
		client.getHostConfiguration().setHost("http://www.tjits.cn/", 80);
		//用目标地址 实例一个POST方法
		PostMethod post = new PostMethod("http://www.tjits.cn/wfcx/vehiclelist.asp");
		//模拟浏览器
		post.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
		//模拟referer来源地址
		post.setRequestHeader("Referer", "http://www.tjits.cn/wfcx/vehiclelist.asp");
		post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=gb2312");
		//将需要的键值对写出来
		NameValuePair lei = new NameValuePair("lei" , "小型汽车");
		NameValuePair provice = new NameValuePair("provice" , "津");
		NameValuePair txtVehicleNo = new NameValuePair("txtVehicleNo", strSerailNum);
		NameValuePair x = new NameValuePair("x" , "");
		NameValuePair y = new NameValuePair("y" , "");

		//给POST方法加入上述键值对
		post.setRequestBody(new NameValuePair[] {lei, provice, txtVehicleNo, x, y});
		//执行POST方法
		try {
			client.executeMethod(post);
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//将POST返回的数据以流的形式读入，再把输入流流至一个buff缓冲字节数组 
		byte[] buff = StreamTool.readInputStream(post.getResponseBodyAsStream());
		//将返回的内容格式化为String存在data中
		String data = new String(buff,"gb2312");
		//任务完成了，释放连接
		post.releaseConnection();
		
		return data;
	}

}
